import { AxiosPromise } from 'axios'
import request from './request'
import { useUserStore } from '@/store/modules/user'

//获取品牌商家列表
export function getListFactory(data) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/factory/getList',
    method: 'post',
    headers: token,
    data,
  })
}
//获取修改品牌商家前的数据查询
export function getBeforeUpdateSelect(id) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/factory/getBeforeUpdateSelect/' + id,
    method: 'get',
    headers: token,
  })
}
// 新增或修改品牌商家
export function factorySave(data) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/factory/save',
    method: 'post',
    headers: token,
    data,
  })
}
//新增或修改品牌商家
export function checkFactory(factoryId, checkType) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/factory/checkFactory/${factoryId}/${checkType}`,
    method: 'put',
    headers: token,
  })
}
// 根据品牌商家ID获取商家详情
export function getFactoryDetail(id) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/factory/getFactoryDetail/' + id,
    method: 'get',
    headers: token,
  })
}
//保存商家调整余额
export function saveFactoryAdjustMoney(data, id) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/factory/saveFactoryAdjustMoney/' + id,
    method: 'post',
    headers: token,
    data,
  })
}

/**
 * 获取商家详情之账号管理
 * @param {string} id 商家id
 * @param {Object} data 请求参数
 * @param {number} data.limit 一页显示多少条
 * @param {string} data.name 关键字
 * @param {number} data.page 当前页码
 */
export function getFactoryEmpAccountList(
  id: string,
  data: { limit: number; name: string; page: number },
) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/factory/getFactoryEmpAccountList/${id}`,
    method: 'post',
    headers: token,
    data,
  })
}

//获取商家详情之结算明细
export function getFactoryOrderModuleSettlementList(data, id) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/factory/getFactoryOrderModuleSettlementList/' + id,
    method: 'post',
    headers: token,
    data,
  })
}
// 获取商家详情之充值记录
export function getFactoryRechargeList(data, id) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/factory/getFactoryRechargeList/' + id,
    method: 'post',
    headers: token,
    data,
  })
}
// 获取品牌厂商全局设置
export function getFactorySet() {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/set/getFactorySet',
    method: 'get',
    headers: token,
  })
}
// 保存厂商设置
export function saveFactorySet(data) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/set/saveFactorySet',
    method: 'put',
    headers: token,
    data,
  })
}

/**
 * 获取品牌商家下拉数据
 * @param data.factoryId - 品牌商家id
 * @param data.limit - 每页显示数量
 * @param data.name - 搜索关键词
 * @param data.page - 当面页码
 */
export function getFactorySelectPage(data: {
  limit: number
  name: string
  page: number
  factoryId: string
}): AxiosPromise<APIResponseList<{ id: string; name: string; select: boolean }>> {
  return request({
    url: '/factory/select/getFactorySelectPage',
    method: 'post',
    data,
  })
}

/**
 * 批量更新开启或关闭工单配件权限
 * @param {boolean} enableSpareAuthori 是否开启工单配件权限：false-关闭，true-开启
 * @param {Array<string>} factoryIds 品牌商家ID
 */
export function updateEnableSpareAuthori(enableSpareAuthori: boolean, factoryIds: Array<string>) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/factory/strategy/updateEnableSpareAuthori?enableSpareAuthori=${enableSpareAuthori}`,
    method: 'post',
    headers: token,
    data: factoryIds,
  })
}

/**
 * 批量更新开启或关闭申请工单配件
 * @param {boolean} enableSpare 是否开启申请工单配件：false-关闭，true-开启
 * @param {Array<string>} factoryIds 品牌商家ID
 */
export function updateEnableApplySpare(enableSpare: boolean, factoryIds: Array<string>) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/factory/strategy/updateEnableApplySpare?enableSpare=${enableSpare}`,
    method: 'post',
    headers: token,
    data: factoryIds,
  })
}

/**
 * 批量更新开启或关闭审核工单配件
 * @param {boolean} enableCheckSpare 是否开启审核工单配件：false-关闭，true-开启
 * @param {Array<string>} factoryIds 品牌商家ID
 */
export function updateEnableCheckSpare(enableCheckSpare: boolean, factoryIds: Array<string>) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/factory/strategy/updateEnableCheckSpare?enableCheckSpare=${enableCheckSpare}`,
    method: 'post',
    headers: token,
    data: factoryIds,
  })
}

/**
 * 批量更新开启或关闭工单费用权限
 * @param {boolean} enableFeeAuthori 是否开启工单费用权限：false-关闭，true-开启
 * @param {Array<string>} factoryIds 品牌商家ID
 */
export function updateEnableFeeAuthori(enableFeeAuthori: boolean, factoryIds: Array<string>) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/factory/strategy/updateEnableFeeAuthori?enableFeeAuthori=${enableFeeAuthori}`,
    method: 'post',
    headers: token,
    data: factoryIds,
  })
}

/**
 * 批量更新开启或关闭申请工单费用
 * @param {boolean} enableFee 是否开启申请工单费用：false-关闭，true-开启
 * @param {Array<string>} factoryIds 品牌商家ID
 */
export function updateEnableApplyFee(enableFee: boolean, factoryIds: Array<string>) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/factory/strategy/updateEnableApplyFee?enableFee=${enableFee}`,
    method: 'post',
    headers: token,
    data: factoryIds,
  })
}

/**
 * 批量更新开启或关闭审核工单费用
 * @param {boolean} enableCheckFee 是否开启审核工单费用：false-关闭，true-开启
 * @param {Array<string>} factoryIds 品牌商家ID
 */
export function updateEnableCheckFee(enableCheckFee: boolean, factoryIds: Array<string>) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/factory/strategy/updateEnableCheckFee?enableCheckFee=${enableCheckFee}`,
    method: 'post',
    headers: token,
    data: factoryIds,
  })
}

/**
 * 批量更新余额不足报单策略
 * @param {1 | 2} orderBalanceStrategy 报单余额策略：1-不限制，2-余额不足禁止下单
 * @param {Array<string>} data 品牌商家ID
 */
export function updateOrderBalanceStrategy(orderBalanceStrategy: 1 | 2, data: Array<string>) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/factory/strategy/updateOrderBalanceStrategy/${orderBalanceStrategy}`,
    method: 'post',
    headers: token,
    data,
  })
}

/**
 * 获取商家详情之工单列表
 * @param {string} id 商家id
 * @param {Object} data 分页配置
 * @param {number} data.limit 每页显示数量
 * @param {number} data.page 当面页码
 */
export const getFactoryDetailOrderList = (id: string, data: { limit: number; page: number }) => {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/factory/getFactoryDetailOrderList/${id}`,
    method: 'post',
    headers: token,
    data,
  })
}

/**
 * 获取商家详情之工单配件列表
 * @param {string} id 商家id
 * @param {Object} data 分页配置
 * @param {number} data.limit 一页显示多少条
 * @param {number} data.page 当面页码
 */
export const getFactoryDetailOrderSpareList = (
  id: string,
  data: { limit: number; page: number },
) => {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/factory/getFactoryDetailOrderSpareList/${id}`,
    method: 'post',
    headers: token,
    data,
  })
}

/**
 * 获取商家详情之工单费用列表
 * @param {string} id 商家id
 * @param {Object} data 分页配置
 * @param {number} data.limit 一页显示多少条
 * @param {number} data.page 当前页码
 */
export const getNetworkDetailOrderFeeList = (id: string, data: { limit: number; page: number }) => {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/factory/getNetworkDetailOrderFeeList/${id}`,
    method: 'post',
    headers: token,
    data,
  })
}
// 获取商家充值审核表
export const getFactoryRechargeCheckList = (data) => {
  return request({
    url: `/factory/getFactoryRechargeCheckList`,
    method: 'post',
    data,
  })
}
//审核商家充值
/**
 * @param {number} checkResult 审核结果：1-通过，2-驳回
 * @param {Array} ids 充值单ID
 */
export const checkFactoryRecharge = (data) => {
  return request({
    url: `/factory/checkFactoryRecharge`,
    method: 'post',
    data,
  })
}
